import {
  Component,
  OnInit,
  ViewChild,
  TemplateRef,
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  EventEmitter,
} from '@angular/core';
import {
  NzMessageService,
  NzModalService,
  UploadXHRArgs,
  UploadFile,
  UploadChangeParam,
} from 'ng-zorro-antd';
import { _HttpClient } from '@delon/theme';
import { tap, map } from 'rxjs/operators';
import { STComponent, STColumn, STData, STChange } from '@delon/abc';
import { WithdrawService } from 'app/my/withdraw/withdraw.service';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import {
  HttpRequest,
  HttpEvent,
  HttpClient,
  HttpEventType,
  HttpResponse,
} from '@angular/common/http';
import { GoodsService } from '../goods.service';
import { ShopService } from 'app/my/shop/shop.service';
import { element } from '@angular/core/src/render3';
import * as _ from 'lodash';

@Component({
  selector: 'tablepanel',
  templateUrl: './tablepanel.component.html',
  styleUrls: ['./tablepanel.component.css'],
})
export class TablepanelComponent implements OnInit {
  showHide = false;
  showHideButton() {
    this.showHide = !this.showHide;
    console.log(this.showHide);
  }

  del(obj: any): any {
    this.modalService.confirm({
      nzTitle: '<i>确认删除么?</i>',
      // nzContent: '<b>确认后微信商户号中的金额会立即转入指定openId的微信余额中！</b>',
      nzOnOk: () => {
        this.goodsService.del(obj.spuId).subscribe(resp => {
          console.log(resp);
          if (resp.success) {
            this.msg.success('操作成功');
            this.getData();
          } else this.msg.error('操作失败');
        });
      },
    });
  }
  openWindow() {
    window.open('http://www.365editor.com/?f=b');
  }

  upOrDownGoods(arg0: number, item: any): any {
    let ids = [];
    ids.push(item.spuId);
    this.goodsService.upOrDownGoods(arg0, ids).subscribe(resp => {
      if (resp.success) {
        this.msg.success('操作成功');
        this.getData();
      }
    });
  }
  fileList = [];
  fileList2 = [];
  previewImage = '';
  previewVisible = false;
  downloadPath = '';
  downloadPath2 = '';
  categoryList = [];
  returnRuleList = [];
  // 商品主图
  photos = [];
  // 规格图
  photos2 = [];
  // 规格分类
  RuleValues = [];
  // 规格详情
  skus: any[] = [];

  onChange = (event: EventEmitter<UploadChangeParam>) => {
    console.log(event);
  };

  handlePreview = (file: UploadFile) => {
    this.previewImage = file.url || file.thumbUrl;
    this.previewVisible = true;
  };

  onRemoveFile = (file: UploadFile) => {
    console.log(file);
    console.log('before: ', this.photos);

    this.photos.forEach(element => {
      if (file.url == element.photoUrl) {
        const index = this.photos.indexOf(element, 0);
        if (index > -1) {
          element.delFlag = 1;
        }
      }
    });

    const index = this.fileList.indexOf(file, 0);
    if (index > -1) {
      this.fileList.splice(index, 1);
    }

    console.log('after: ', this.photos);
  };

  onRemoveFile2 = (file: UploadFile) => {
    console.log(file);
    console.log('before: ', this.photos2);

    this.photos2.forEach(element => {
      if (file.url == element.url) {
        const index = this.photos2.indexOf(element, 0);
        if (index > -1) {
          element.delFlag = 1;
        }
      }
    });

    const index = this.fileList2.indexOf(file, 0);
    if (index > -1) {
      this.fileList2.splice(index, 1);
    }

    console.log('after: ', this.photos2);
  };

  // 自定义文件上传
  customReq = (item: UploadXHRArgs) => {
    // 构建一个 FormData 对象，用于存储文件或其他参数
    const formData = new FormData();
    // tslint:disable-next-line:no-any
    formData.append('file', item.file as any);
    formData.append('id', '1000');
    const req = new HttpRequest('POST', item.action, formData, {
      reportProgress: true,
      withCredentials: true,
    });

    // 始终返回一个 `Subscription` 对象，nz-upload 会在适当时机自动取消订阅
    return this._http.request(req).subscribe(
      (event: HttpEvent<{}>) => {
        if (event.type === HttpEventType.UploadProgress) {
          if (event.total > 0) {
            // tslint:disable-next-line:no-any
            (event as any).percent = (event.loaded / event.total) * 100;
          }
          // 处理上传进度条，必须指定 `percent` 属性来表示进度
          item.onProgress(event, item.file);
        } else if (event instanceof HttpResponse) {
          // 处理成功
          item.onSuccess(event.body, item.file, event);
          let obj = event.body as any;
          let obj2 = item.file as any;

          this.downloadPath = obj.downloadPath;
          this.photos.push({
            uid: obj2.uid,
            photoUrl: obj.downloadPath,
            sort: 0,
            type: 1,
            delFlag: 0,
          });
        }
      },
      err => {
        // 处理失败
        item.onError(err, item.file);
      },
    );
  };

  customReq2 = (item: UploadXHRArgs) => {
    // 构建一个 FormData 对象，用于存储文件或其他参数
    const formData = new FormData();
    // tslint:disable-next-line:no-any
    formData.append('file', item.file as any);
    formData.append('id', '1000');
    const req = new HttpRequest('POST', item.action, formData, {
      reportProgress: true,
      withCredentials: true,
    });

    // 始终返回一个 `Subscription` 对象，nz-upload 会在适当时机自动取消订阅
    return this._http.request(req).subscribe(
      (event: HttpEvent<{}>) => {
        if (event.type === HttpEventType.UploadProgress) {
          if (event.total > 0) {
            // tslint:disable-next-line:no-any
            (event as any).percent = (event.loaded / event.total) * 100;
          }
          // 处理上传进度条，必须指定 `percent` 属性来表示进度
          item.onProgress(event, item.file);
        } else if (event instanceof HttpResponse) {
          // 处理成功
          item.onSuccess(event.body, item.file, event);
          let obj = event.body as any;
          let obj2 = item.file as any;

          this.downloadPath2 = obj.downloadPath;
          this.photos2.push({
            url: obj.downloadPath,
            sort: 0,
            type: 1,
            delFlag: 0,
          });
        }
      },
      err => {
        // 处理失败
        item.onError(err, item.file);
      },
    );
  };

  q: any = {
    pi: 1,
    ps: 10,
    sorter: '',
    status: null,
    statusList: [],
  };
  data: any[] = [];
  loading = false;
  status = [
    { index: 0, text: '关闭', value: false, type: 'default', checked: false },
    {
      index: 6,
      text: '运行中',
      value: false,
      type: 'processing',
      checked: false,
    },
    { index: 2, text: '已上线', value: false, type: 'success', checked: false },
    { index: 97, text: '异常', value: false, type: 'error', checked: false },
  ];
  @ViewChild('st')
  st: STComponent;
  columns: STColumn[] = [
    { title: '', index: 'key', type: 'checkbox' },
    { title: '序号', index: '_index' },
    { title: 'SPU', index: 'spuId' },
    { title: '分类', index: 'category' },
    { title: '名称', index: 'spuName', width: '200px' },
    { title: '图片', index: 'photo', type: 'img' },
    { title: '会员价', index: 'listPrice' },
    { title: '市场价(元)', index: 'listPrice' },
    { title: '销售价(元)', index: 'unitPrice' },
    { title: '库存', index: 'quantity' },
    { title: '状态', index: 'statusName' },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          click: (item: any) => this.edit(this.modalContent, item),
        },
        {
          text: '删除',
          click: (item: any) => this.del(item),
        },
        {
          text: '上架',
          click: (item: any) => this.upOrDownGoods(1, item),
        },
        {
          text: '下架',
          click: (item: any) => this.upOrDownGoods(2, item),
        },
      ],
    },
  ];
  selectedRows: STData[] = [];
  description = '';
  totalCallNo = 0;
  expandForm = false;

  columns4Standard: STColumn[] = [
    { title: 'SKU', index: 'skuId' },
    { title: '规格组合', index: 'name', width: '200px' },
    { title: '图片', index: 'photoUrl', type: 'img' },
    {
      title: '市场价(元)',
      index: 'listPrice',
      render: 'listPrice',
    },
    {
      title: '销售价(元)',
      index: 'unitPrice',
      render: 'unitPrice',
    },
    {
      title: '会员价(元)',
      index: 'memberPrice',
    },
    {
      title: '积分',
      index: 'integral',
    },
    {
      title: '支付方式',
      index: 'radioValue',
    },
    { title: '库存', index: 'quantity' },
    { title: '删除标记', index: 'delFlag' },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          click: (item: any) =>this.editSpecification(this.modalContent2, item),
        },
        {
          text: '删除',
          click: (item: any) => this.remove4guig(item),
        },
      ],
    },
  ];

  columns4RuleValues: STColumn[] = [
    { title: 'id', index: 'id' },
    { title: '规格分类', index: 'ruleName' },
    { title: '规格值', index: 'value' },
    { title: '删除标记', index: 'delFlag' },
    {
      title: '操作',
      buttons: [
        {
          text: '删除',
          click: (item: any) => this.remove4RuleValues(item),
        },
      ],
    },
  ];

  constructor(
    private http: _HttpClient,
    public msg: NzMessageService,
    private modalSrv: NzModalService,
    private cdr: ChangeDetectorRef,
    private goodsService: GoodsService,
    private modalService: NzModalService,
    private fb: FormBuilder,
    private _http: HttpClient,
    private _shopServer: ShopService,
  ) {}

  // 去重
  unique(arr) {
    return _.uniqBy(arr, 'id');
  }

  _ruleValues: any[] = [];

  // 提交编辑
  edit(tpl: TemplateRef<{}>, item: any) {
    this.deleteRuleValues = false;

    // spuid查详情
    this.goodsService.getSpuDetail(item.spuId).subscribe(
      resp => {
        console.log(resp);
        this.form.patchValue({
          spuName: resp.data.spuName == undefined ? "" : resp.data.spuName,
          categoryId: resp.data.categoryId,
          info: resp.data.info,
        });

        this.fileList = [];
        this.photos = [];
        resp.data.photos.forEach(element => {
          this.fileList.push({ url: element.photoUrl });
          this.photos.push({
            id: element.id,
            photoUrl: element.photoUrl,
            sort: 0,
            type: 1,
            delFlag: 0,
          });
        });

        this.fileList2 = [];
        this.photos2 = [];

        this._ruleValues = resp.data.RuleValues;

        if (resp.data.skus != null) {
          this.skus = [];
          resp.data.skus.forEach((element, index) => {
            let newElement = element;

            newElement.name = '';
            element.RuleValues.forEach(element2 => {
              newElement.name +=
                element2.ruleName + ' ' + element2.value + '; ';
            });

            if (element.photos.length > 0)
              newElement.photoUrl = element.photos[0].photoUrl;

            newElement.listPrice = Number(newElement.listPrice).toFixed(0);
            newElement.unitPrice = Number(newElement.unitPrice).toFixed(0);
            newElement.costPrice = Number(newElement.unitPrice).toFixed(0);

            // this.fileList2.push({ url: newElement.photoUrl });
            element.photos.forEach(element => {
              this.fileList2.push({ url: element.photoUrl });
              this.photos2.push({ id: element.id, url: element.photoUrl });
            });

            this.skus.push(newElement);
          });
        }
      },
      error => {
        console.log(error);
      },
    );

    this.goodsService.getSpuaftersaleConf(item.spuId).subscribe(
      resp => {
        console.log(resp);
        if (resp.success) {
          this.form.patchValue({
            returnRuleId: resp.data.id,
          });
        }
      },
      error => {
        console.log(error);
      },
    );

    this.modalSrv.create({
      nzKeyboard: false,
      nzMaskClosable: false,
      nzWidth: 1300,
      nzTitle: '编辑商品',
      nzContent: tpl,
      nzOnOk: () => {
        for (const i in this.form.controls) {
          this.form.controls[i].markAsDirty();
          this.form.controls[i].updateValueAndValidity();
        }
        if (this.form.invalid) return false;

        this.form.value.photos = this.photos;
        this.form.value.skus = this.skus;

        let tmp = [];
        this.skus.forEach(element => {
          element.RuleValues.forEach(element2 => {
            // element2.delFlag = element.delFlag;
            tmp.push(element2);
          });
        });

        // 外层的 RuleValues
        if(this.deleteRuleValues)
          // 手工删除
          this.form.value.RuleValues = this._ruleValues;
        else
          // 遍历skus生产
          this.form.value.RuleValues = this.unique(tmp);

        this.form.value.status = 1;
        this.form.value.type = 1;

        this.form.value.spuId = item.spuId;

        this.loading = true;
        this.goodsService.createOrEdit(this.form.value).subscribe(
          resp => {
            this.loading = false;
            if (resp.success) {
              this.goodsService
                .setSpuRela([resp.data], this.form.value.returnRuleId)
                .subscribe(
                  resp => {
                    console.log(resp);

                    let data = [];
                    // 遍历sku列表，给每个商品设置会员价、积分、支付方式
                    this.form.value.skus.forEach(element => {
                      data.push({
                        list: [
                          {
                            id: null,
                            type: element.radioValue,
                            mlv: 1,
                            mLv: 1,
                            price: element.memberPrice,
                            integral: element.integral,
                          },
                        ],
                        skuId: element.skuId,
                        type: element.radioValue
                      });
                    });

                    this.goodsService.batchSetMemberPriceAndIntegral(data).subscribe(resp => {
                      console.log("setMemberPriceAndIntegral: " + resp);
                    },
                    error => {
                      console.log(error);
                    });

                    this.msg.success('操作成功');
                    this.getData();
                  },
                  error => {
                    console.log(error);
                  },
                );
            } else this.msg.error('异常: ' + resp.bizMessage);
          },
          error => {
            this.loading = false;
            this.msg.error('异常: ' + JSON.stringify(error));
          },
        );

        return false;
      },
      nzCancelText: '取消',
    });
  }

  form: FormGroup;
  form2: FormGroup;
  submitting = false;

  ngOnInit() {
    this.getData();

    this.form = this.fb.group({
      spuName: [null, [Validators.required]],
      categoryId: [null, [Validators.required]],
      returnRuleId: [null, [Validators.required]],
      info: [null],
    });

    this.form2 = this.fb.group({
      skuId: [null],
      ruleName: [null, [Validators.required]],
      value: [null, [Validators.required]],
      ruleName2: [null],
      value2: [null],
      ruleName3: [null],
      value3: [null],
      listPrice: [null, [Validators.required]],
      unitPrice: [null, [Validators.required]],
      quantity: [null, [Validators.required]],
      radioValue: [null],
      memberPrice: [null],
      integral: [null],
    });

    this.goodsService.queryCategoryList().subscribe(resp => {
      if (resp.success) {
        this.categoryList = resp.data.list;
      }
    });

    this._shopServer.queryReturnedRuleList().subscribe(resp => {
      if (resp.success) {
        resp.data.forEach(element => {
          element.name =
            element.returnedDay + '天退货, ' + element.exchangeDay + '天换货';
          this.returnRuleList.push(element);
        });
      }
    });
  }

  getData() {
    this.loading = true;
    this.q.statusList = this.status
      .filter(w => w.checked)
      .map(item => item.index);

    if (this.q.status !== null && this.q.status > -1)
      this.q.statusList.push(this.q.status);

    this.loading = false;

    this.goodsService.queryList(null).subscribe(resp => {
      if (resp.success) {
        let obj;
        this.data = [];
        let count = 0;
        resp.data.list.forEach(element => {
          obj = element;

          if (obj.status == 1) obj.statusName = '上架';

          if (obj.status == 2) obj.statusName = '下架';

          obj._index = ++count;

          this.data.push(obj);
        });

        console.log(this.data);
      }
    });
  }

  stChange(e: STChange) {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox;
        this.totalCallNo = this.selectedRows.reduce(
          (total, cv) => total + cv.callNo,
          0,
        );
        this.cdr.detectChanges();
        break;
      case 'filter':
        this.getData();
        break;
    }
  }

  remove() {
    this.http
      .delete('/rule', { nos: this.selectedRows.map(i => i.no).join(',') })
      .subscribe(() => {
        this.getData();
        this.st.clearCheck();
      });
  }

  remove4guig(obj) {
    const index = this.skus.indexOf(obj, 0);
    if (index > -1) {
      if (this.skus[index].delFlag == 0) this.skus[index].delFlag = 1;
      else this.skus[index].delFlag = 0;
    }

    let tmp = this.skus;
    this.skus = [];
    tmp.forEach(element => {
      this.skus.push(element);
    });
  }

  deleteRuleValues = false;

  // 规格列表删除
  remove4RuleValues(obj) {
    const index = this._ruleValues.indexOf(obj, 0);
    if (index > -1) {
      if (this._ruleValues[index].delFlag == 0)
        this._ruleValues[index].delFlag = 1;
      else this._ruleValues[index].delFlag = 0;
    }

    let tmp = this._ruleValues;
    this._ruleValues = [];
    tmp.forEach(element => {
      this._ruleValues.push(element);
    });

    this.deleteRuleValues = true;
  }

  approval() {
    this.msg.success(`审批了 ${this.selectedRows.length} 笔`);
  }

  add(tpl: TemplateRef<{}>) {
    this.form.reset();
    this.fileList = [];
    this.fileList2 = [];
    this.skus = [];
    this.photos = [];

    this.modalSrv.create({
      nzKeyboard: false,
      nzMaskClosable: false,
      nzWidth: 1300,
      nzTitle: '新建商品',
      nzContent: tpl,
      nzOnOk: () => {
        for (const i in this.form.controls) {
          this.form.controls[i].markAsDirty();
          this.form.controls[i].updateValueAndValidity();
        }
        if (this.form.invalid) return false;

        this.form.value.photos = this.photos;
        this.form.value.skus = this.skus;

        let tmp = [];
        this.skus.forEach(element => {
          element.RuleValues.forEach(element2 => {
            element2.delFlag = element.delFlag;
            tmp.push(element2);
          });
        });

        this.form.value.RuleValues = tmp;
        this.form.value.status = 1;
        this.form.value.type = 1;

        console.log(this.form.value);

        this.loading = true;
        this.goodsService.createOrEdit(this.form.value).subscribe(
          resp => {
            this.loading = false;
            if (resp.success) {
              this.goodsService
                .setSpuRela([resp.data], this.form.value.returnRuleId)
                .subscribe(
                  resp => {
                    console.log(resp);
                    this.msg.success('操作成功');
                    this.getData();
                  },
                  error => {
                    console.log(error);
                  },
                );
            } else this.msg.error('异常: ' + resp.bizMessage);
          },
          error => {
            this.loading = false;
            this.msg.error('异常: ' + JSON.stringify(error));
          },
        );

        return false;
      },
      nzCancelText: '取消',
    });
  }

  addSpecification(tpl: TemplateRef<{}>) {
    this.form2.reset();
    this.fileList2 = [];

    this.form2.patchValue({
      radioValue: '1',
    });

    this.modalSrv.create({
      nzKeyboard: false,
      nzMaskClosable: false,
      nzWidth: 800,
      nzTitle: '新建规格',
      nzContent: tpl,
      nzOnOk: () => {
        for (const i in this.form2.controls) {
          this.form2.controls[i].markAsDirty();
          this.form2.controls[i].updateValueAndValidity();
        }
        if (this.form2.invalid) return false;

        let tmp = this.skus;
        this.skus = [];

        let RuleValues = [];
        RuleValues.push({
          delFlag: 0,
          ruleName: this.form2.value.ruleName,
          value: this.form2.value.value,
        });

        if (
          this.form2.value.ruleName2 != null &&
          this.form2.value.ruleName2 != ''
        ) {
          RuleValues.push({
            delFlag: 0,
            ruleName: this.form2.value.ruleName2,
            value: this.form2.value.value2,
          });
        }
        if (
          this.form2.value.ruleName3 != null &&
          this.form2.value.ruleName3 != ''
        ) {
          RuleValues.push({
            delFlag: 0,
            ruleName: this.form2.value.ruleName3,
            value: this.form2.value.value3,
          });
        }

        this.form2.value.RuleValues = RuleValues;
        let name = '';
        RuleValues.forEach(element => {
          name += element.ruleName + ' ' + element.value + '; ';
        });
        this.form2.value.name = name;

        let photos = [];
        photos.push({ photoUrl: this.downloadPath, sort: 0, type: 1 });
        this.form2.value.photos = photos;
        this.form2.value.photoUrl = this.downloadPath;
        this.form2.value.delFlag = 0;
        this.skus.push(this.form2.value);
        tmp.forEach(element => {
          this.skus.push(element);
        });
        this.skus.reverse();

        this.cdr.detectChanges();
        this.cdr.markForCheck();

        this.msg.success(`操作成功`);

        return false;
      },
      nzCancelText: '取消',
    });
  }

  reset() {
    // wait form reset updated finished
    setTimeout(() => this.getData());
  }

  @ViewChild('modalContent')
  modalContent: TemplateRef<{}>;

  @ViewChild('modalContent2')
  modalContent2: TemplateRef<{}>;

  editSpecification(tpl: TemplateRef<{}>, item: any) {
    this.form2.patchValue({
      skuId: item.skuId,

      ruleName: item.RuleValues.length > 0 ? item.RuleValues[0].ruleName : null,
      value: item.RuleValues.length > 0 ? item.RuleValues[0].value : null,
      ruleName2:
        item.RuleValues.length > 1 ? item.RuleValues[1].ruleName : null,
      value2: item.RuleValues.length > 1 ? item.RuleValues[1].value : null,
      ruleName3:
        item.RuleValues.length > 2 ? item.RuleValues[2].ruleName : null,
      value3: item.RuleValues.length > 2 ? item.RuleValues[2].value : null,

      listPrice: Number(item.listPrice).toFixed(0),
      unitPrice: Number(item.unitPrice).toFixed(0),
      quantity: item.quantity,

      radioValue: '1',
    });

    this.modalSrv.create({
      nzKeyboard: false,
      nzMaskClosable: false,
      nzWidth: 800,
      nzTitle: '编辑规格',
      nzContent: tpl,
      nzOnOk: () => {
        for (const i in this.form2.controls) {
          this.form2.controls[i].markAsDirty();
          this.form2.controls[i].updateValueAndValidity();
        }
        if (this.form2.invalid) return false;

        // 删除原有规格
        // item.RuleValues.forEach(element => {
        //   element.delFlag = 1;
        // });
        let RuleValues = item.RuleValues;

        if (RuleValues.length > 0) {
          RuleValues[0].ruleName = this.form2.value.ruleName;
          RuleValues[0].value = this.form2.value.value;
          
          if (RuleValues.length > 1) {
            RuleValues[1].ruleName = this.form2.value.ruleName2;
            RuleValues[1].value = this.form2.value.value2;
          }

          if (RuleValues.length > 2) {
            RuleValues[2].ruleName = this.form2.value.ruleName3;
            RuleValues[2].value = this.form2.value.value3;
          }

        } else {
          RuleValues.push({
            delFlag: 0,
            ruleName: this.form2.value.ruleName,
            value: this.form2.value.value,
          });

          if (
            this.form2.value.ruleName2 != null &&
            this.form2.value.ruleName2 != ''
          ) {
            RuleValues.push({
              delFlag: 0,
              ruleName: this.form2.value.ruleName2,
              value: this.form2.value.value2,
            });
          }
          if (
            this.form2.value.ruleName3 != null &&
            this.form2.value.ruleName3 != ''
          ) {
            RuleValues.push({
              delFlag: 0,
              ruleName: this.form2.value.ruleName3,
              value: this.form2.value.value3,
            });
          }
        }

        this.form2.value.RuleValues = RuleValues;
        let name = '';
        RuleValues.forEach(element => {
          if (element.delFlag == 0)
            name += element.ruleName + ' ' + element.value + '; ';
        });
        this.form2.value.name = name;

        // 规格图片
        let photos = [];
        this.photos2.forEach(element => {
          photos.push({
            id: element.id,
            photoUrl: element.url,
            sort: 0,
            type: 1,
            delFlag: element.delFlag == undefined ? 0 : element.delFlag,
          });
        });

        this.form2.value.photos = photos;

        this.form2.value.photoUrl =
          this.photos2.length > 0 ? this.photos2[0].url : null;

        // 如果外面点了删除会覆盖这个值
        this.form2.value.delFlag = 0;
        // 更新sku匹配的值
        let tmp = this.skus;
        tmp.forEach((element, index) => {
          if (element.skuId == item.skuId) {
            tmp[index] = this.form2.value;
            // 防止id被覆盖
            tmp[index].skuId = item.skuId;
            console.log("new sku: " + JSON.stringify(tmp[index]));
          }
        });

        this.skus = [];
        tmp.forEach(element => {
          this.skus.push(element);
        });

        this.cdr.detectChanges();
        this.cdr.markForCheck();

        this.msg.success(`操作成功`);
        return true;
      },
      nzCancelText: '取消',
    });
  }

  showRuleValues(tpl: TemplateRef<{}>) {
    this.modalSrv.create({
      nzKeyboard: false,
      nzMaskClosable: false,
      nzWidth: 800,
      nzTitle: '编辑规格列表',
      nzContent: tpl,
      nzOnOk: () => {
        return true;
      },
      nzCancelText: '取消',
    });
  }
}
